import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/Login/index.vue'
import { useUserStore } from '@/stores'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// import 
// 插件配置，修改颜色
NProgress.configure({
  showSpinner: false
})

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
   {
      path:'/login',
      name:'login',
      component:()=>import('@/views/Login/index.vue')
   },
   
   {
    path:'/',
    component:()=>import('@/views/Layout/index.vue'),
    redirect:'/home',
    children:[
 {
  path:'/home',
 meta:{title:'首页'},
  component:()=>import('@/views/Home/index.vue')
},
{
  path:'/article',
 meta:{title:'健康百科'},
  
  component:()=>import('@/views/Article/index.vue')
},
{
  path:'/notify',
 meta:{title:'消息中心'},
  
  component:()=>import('@/views/Notify/index.vue')
},
{
  path:'/user',
  meta:{title:'我的'},

  component:()=>import('@/views/User/index.vue')

},
{
  path: '/user/patient',
  component: () => import('@/views/User/PatientPage.vue'),
  meta: { title: '家庭档案' }
},
{
  path: '/consult/fast',
  component: () => import('@/views/Consult/ConsultFast.vue'),
  meta: { title: '极速问诊' }
},
{
  path: '/consult/dep',
  component: () => import('@/views/Consult/ConsultDep.vue'),
  meta: { title: '选择科室' }
},
{
  path: '/consult/illness',
  component: () => import('@/views/Consult/ConsultIllness.vue'),
  meta: { title: '病情描述' }
},
{
  path: '/consult/pay',
  component: () => import('@/views/Consult/ConsultPay.vue'),
  meta: { title: '问诊支付' }
}



  ]

}

  ]
})
// vue2的导航守卫：
// router.beforeEach((to,from,next)=>{

// })

// vue3的导航守卫：
// router.beforeEach((to)=>{
//   to:是要访问的页面
//   如果没有权限（没有token）,只要return '/login':表示返回到登录页面
//   如果没有token 的话就什么都不做，表示放行
// })
// 访问权限控制
router.beforeEach((to) => {
  NProgress.start()
  // 用户仓库
  const store = useUserStore()
  // 不需要登录的页面，白名单
  const wihteList = ['/login']
  // 如果没有登录且不在白名单内，去登录
  if (!store.user?.token && !wihteList.includes(to.path)) return '/login'
  // 否则不做任何处理
})

router.afterEach((to)=>{
  document.title=`${to.meta.title||''}--优医问诊`
  NProgress.done()
})
export default router
